import {Component} from '@angular/core';
import {Platform} from 'ionic-angular';
import {StatusBar} from '@ionic-native/status-bar';
import {SplashScreen} from '@ionic-native/splash-screen';
import {TranslateService} from "@ngx-translate/core";
import {Network} from "@ionic-native/network";
import {JPush} from '@jiguang-ionic/jpush';

@Component({
    templateUrl: 'app.html'
})
export class MyApp {
    rootPage: any;

    constructor(platform: Platform,
                statusBar: StatusBar,
                splashScreen: SplashScreen,
                public translateService: TranslateService,
                private network: Network,
                jPush: JPush) {
        platform.ready().then(() => {
            // Okay, so the platform is ready and our plugins are available.
            // Here you can do any higher level native things you might need.
            statusBar.styleDefault();
            splashScreen.hide();

            jPush.init();
            jPush.setDebugMode(true);

            this.setRootPage();
            this.setLang();
            this.netWatch();
        });
    }

    netWatch() {
        let connectSubscription = this.network.onConnect().subscribe(() => {
            console.log('network connected!');
            // We just got a connection but we need to wait briefly
            // before we determine the connection type. Might need to wait.
            // prior to doing any api requests as well.
            setTimeout(() => {
                console.log(this.network);
            }, 3000);
        });
    }

    //设置根路径
    private setRootPage() {
        // localStorage.getItem('access_token') ? this.rootPage = 'TabsPage' : this.rootPage = 'LoginPage';
        this.rootPage = 'TabsPage';
    }

    //设置语言
    private setLang() {
        this.translateService.addLangs(["zh", "en"]);
        this.translateService.setDefaultLang("zh");
        let browserLang = localStorage.getItem('lang') || this.translateService.getBrowserLang();
        localStorage.setItem('lang', browserLang);
        this.translateService.use(browserLang.match(/zh|en/) ? browserLang : 'zh');
    }

}
